/**
 * 
 */
package de.wesemann.mediahandbook.le.activity.helper;

import android.os.AsyncTask;

/**
 * @author Lars W.
 * 
 */
public abstract class AbstractAsyncTask<Params, Progress, Result> extends AsyncTask<Params, Progress, Result> {
	private boolean completed;
	protected ActivityHelper activity;
	private int reloadTries = 0;

	protected AbstractAsyncTask(ActivityHelper activity) {
		this.activity = activity;
	}

	public void setActivity(ActivityHelper activity) {
		this.activity = activity;
		if (completed) {
			notifyActivityTaskCompleted();
		}
	}

	@Override
	protected void onPostExecute(Result result) {
		// companiesInTheContainer.setData(foundCompanies);
		// foundCompanies = null;
		completed = true;
		notifyActivityTaskCompleted();
	}

	/**
	 * Helper method to notify the activity that this task was completed.
	 */
	private void notifyActivityTaskCompleted() {
		if (null != activity) {
			activity.onTaskCompleted(this);
		}
	}

	@Override
	protected void onCancelled() {
		super.onCancelled();
		// TODO - should be reloaded!
		// and say something like: 'try again' with Toast
		// Try only 2 times then break
		reloadTries++;
		activity.reload(this);
	}

	public int getreLoadTries() {
		return reloadTries;
	}
	
}
